﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using BaseThing.Game;
using BaseThing.Definitions;

namespace BaseThing.Engine
{
    class GUIManager
    {
        private Dictionary<int, GUIBaseScreen> ScreenDictionary;
        private GUIBaseScreen currentScreen;
        private GameStates previousGameState = GameStates.GAME_STATE_NEW_GAME;

        public GUIBaseScreen CurrentScreen
        {
            get { return currentScreen; }
        }

        public GUIManager()
        {
            //Establecemos el estado previo inicial
            previousGameState = GameStates.GAME_STATE_SPLASH;

            ScreenDictionary = new Dictionary<int,GUIBaseScreen>();

            //Splash
            this.CreateSplashScreen(ref currentScreen);
            ScreenDictionary.Add((int)GameStates.GAME_STATE_SPLASH, currentScreen);

            //New Game
            this.CreateNewGameScreen(ref currentScreen);
            ScreenDictionary.Add((int)GameStates.GAME_STATE_NEW_GAME, currentScreen);

            //Init
            this.CreateInitScreen(ref currentScreen);
            ScreenDictionary.Add((int)GameStates.GAME_STATE_INIT, currentScreen);

            //Loading
            this.CreateLoadingScreen(ref currentScreen);
            ScreenDictionary.Add((int)GameStates.GAME_STATE_LOADING, currentScreen);

            //Playing
            this.CreatePlayingScreen(ref currentScreen);
            ScreenDictionary.Add((int)GameStates.GAME_STATE_PLAYING, currentScreen);

            //Pause
            this.CreatePauseScreen(ref currentScreen);
            ScreenDictionary.Add((int)GameStates.GAME_STATE_PAUSE, currentScreen);

            //End
            this.CreateEndScreen(ref currentScreen);
            ScreenDictionary.Add((int)GameStates.GAME_STATE_END, currentScreen);

            //Exit
            this.CreateSplashScreen(ref currentScreen);
            ScreenDictionary.Add((int)GameStates.GAME_STATE_EXIT, currentScreen);

            //Establece la Splash Screen como pantalla inicial
            currentScreen = ScreenDictionary[(int) previousGameState];
        }

        public void Update(GameTime gameTime, ref GameStates gameState)
        {
            if (previousGameState != gameState)
            {
                //Activa la pantalla segun el estado del juego
                if (ScreenDictionary.TryGetValue((int)gameState, out currentScreen))
                {
                    currentScreen = ScreenDictionary[(int)gameState];
                }
                else
                {
                    currentScreen = ScreenDictionary[(int)GameStates.GAME_STATE_NEW_GAME];
                }

                previousGameState = gameState;
            }

            //Actualiza la Screen actual
            currentScreen.Update(gameTime, ref gameState);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            //Dibuja la Screen actual
            currentScreen.Draw(spriteBatch);
        }

        //Hacer un create para cada tipo de pantalla que usemos.
        private void CreateSplashScreen(ref GUIBaseScreen s)
        {
            GUISplashScreen sc = new GUISplashScreen();

            sc.BackGround = new Sprite(@"Textures\walk", 8, 1, 3);
            sc.BackColor = Color.Black;

            s = sc;
        }

        private void CreateNewGameScreen(ref GUIBaseScreen s)
        {
            GUINewGameScreen sc = new GUINewGameScreen();

            sc.BackGround = new Sprite(@"Textures\walk", 8, 1, 3);
            sc.BackColor = Color.Black;

            s = sc;
        }

        private void CreateInitScreen(ref GUIBaseScreen s)
        {
            GUIInitScreen sc = new GUIInitScreen();

            sc.BackGround = new Sprite(@"Textures\walk", 8, 1, 3);
            sc.BackColor = Color.Black;

            s = sc;
        }

        private void CreateLoadingScreen(ref GUIBaseScreen s)
        {
            GUILoadingScreen sc = new GUILoadingScreen();

            sc.BackGround = new Sprite(@"Textures\walk", 8, 1, 3);
            sc.BackColor = Color.Black;

            s = sc;
        }

        private void CreatePlayingScreen(ref GUIBaseScreen s)
        {
            GUIPlayingScreen sc = new GUIPlayingScreen();

            sc.BackGround = new Sprite(@"Textures\walk", 8, 1, 3);
            sc.BackColor = Color.Black;

            s = sc;
        }

        private void CreatePauseScreen(ref GUIBaseScreen s)
        {
            GUIPauseScreen sc = new GUIPauseScreen();

            sc.BackGround = new Sprite(@"Backgrounds\GamePaused", 1, 1, 1);
            sc.BackGround.Alpha = 177;

            sc.BackColor = Color.Black;

            s = sc;
        }

        private void CreateEndScreen(ref GUIBaseScreen s)
        {
            GUIEndScreen sc = new GUIEndScreen();

            sc.BackGround = new Sprite(@"Textures\walk", 8, 1, 3);
            sc.BackColor = Color.Black;

            s = sc;
        }

        private void CreateExitScreen(ref GUIBaseScreen s)
        {
            GUIExitScreen sc = new GUIExitScreen();

            sc.BackGround = new Sprite(@"Textures\walk", 8, 1, 3);
            sc.BackColor = Color.Black;

            s = sc;
        }
    }
}
